shell练习-telnet 测试

telnet 批量测试端口

while,第一种,读取文件

host内容为:ip 端口

范例host.txt:192.168.0.1 22

1
2
3
4
5
6
7
8
9
while read ip
do
expect <<EOF
set timeout 3
spawn telnet $ip
expect "Escape character" { send "\1D\r quit\r"  }
expect eof
EOF
done < host.txt

while,第二种写在脚本里

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
while read ip
do
expect <<EOF
set timeout 3
spawn telnet $ip
expect "Escape character" { send "\1D\r quit\r"  }
expect eof
EOF
done <<EOF
192.168.232.15 22
192.168.232.16 22
192.168.232.17 22
127.0.0.1 22
EOF

for ,读取host.txt,host内只有IP!!!没有端口!!!注意for读取时以空格为分割符,因此如果是一行多个字段独立的,建议使用while()

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
for i in $(<ip.txt)
do
expect <<EOF |grep Connected
set timeout 3;
spawn telnet $i 22
expect "Escape character" { send "\1D\r quit\r "  }
expect eof

EOF
done